using System;
using EPiServer.Common;
using EPiServer.Common.Queries;
using EPiServer.Common.Reporting;
using EPiServer.Common.Security;

namespace EPiAbstractions.Common.Reporting
{
    public class ReportHandlerFacade : IReportHandlerFacade
    {
        private static ReportHandlerFacade _instance;

        public ReportHandlerFacade()
        {
            ReportHandler.ReportAdded += OnReportAdded;
            ReportHandler.ReportUpdated += OnReportUpdated;
            ReportHandler.ReportRemoved += OnReportRemoved;
            ReportHandler.ReportCaseUpdated += OnReportCaseUpdated;
            ReportHandler.ReportCaseRemoved += OnReportCaseRemoved;
        }

        public static ReportHandlerFacade Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new ReportHandlerFacade();

                return _instance;
            }

            set { _instance = value; }
        }

        #region IReportHandlerFacade Members

        public event EPiServerCommonEventHandler ReportAdded;
        public event EPiServerCommonEventHandler ReportUpdated;
        public event EPiServerCommonEventHandler ReportRemoved;
        public event EPiServerCommonEventHandler ReportCaseUpdated;
        public event EPiServerCommonEventHandler ReportCaseRemoved;

        public virtual IXmlReportArchiveProvider GetArchiveProvider(String elementName)
        {
            return ReportHandler.GetArchiveProvider(elementName);
        }

        public virtual IReport GetReport(Int32 id)
        {
            return ReportHandler.GetReport(id);
        }

        public virtual IReport AddReport(IReport report)
        {
            return ReportHandler.AddReport(report);
        }

        public virtual void UpdateReport(IReport report)
        {
            ReportHandler.UpdateReport(report);
        }

        public virtual void RemoveReport(IReport report)
        {
            ReportHandler.RemoveReport(report);
        }

        public virtual ReportCollection GetReports(IReportCase reportCase, Int32 page, Int32 pageSize, out Int32 totalItems,
                                                   ReportSortOrder[] sortOrder)
        {
            return ReportHandler.GetReports(reportCase, page, pageSize, out totalItems, sortOrder);
        }

        public virtual IReportCase GetReportCase(Int32 id)
        {
            return ReportHandler.GetReportCase(id);
        }

        public virtual void UpdateReportCase(IReportCase reportCase)
        {
            ReportHandler.UpdateReportCase(reportCase);
        }

        public virtual void RemoveReportCase(IReportCase reportCase)
        {
            ReportHandler.RemoveReportCase(reportCase);
        }

        public virtual ReportCaseCollection GetReportCases(SiteCollection sites, Int32 page, Int32 pageSize, out Int32 totalItems,
                                                           ReportCaseSortOrder[] sortOrder)
        {
            return ReportHandler.GetReportCases(sites, page, pageSize, out totalItems, sortOrder);
        }

        public virtual ReportCaseCollection GetReportCases(SiteCollection sites, NullValueAction siteNullAction, Int32 page, Int32 pageSize,
                                                           out Int32 totalItems, ReportCaseSortOrder[] sortOrder)
        {
            return ReportHandler.GetReportCases(sites, siteNullAction, page, pageSize, out totalItems, sortOrder);
        }

        public virtual ReportCaseCollection GetReportCases(ReportCaseStatus status, SiteCollection sites, Int32 page, Int32 pageSize,
                                                           out Int32 totalItems, ReportCaseSortOrder[] sortOrder)
        {
            return ReportHandler.GetReportCases(status, sites, page, pageSize, out totalItems, sortOrder);
        }

        public virtual ReportCaseCollection GetReportCases(ReportCaseStatus status, SiteCollection sites, NullValueAction siteNullAction,
                                                           Int32 page, Int32 pageSize, out Int32 totalItems, ReportCaseSortOrder[] sortOrder)
        {
            return ReportHandler.GetReportCases(status, sites, siteNullAction, page, pageSize, out totalItems, sortOrder);
        }

        public virtual ReportCaseCollection GetReportCases(IReportableEntity entity, SiteCollection sites, Int32 page, Int32 pageSize,
                                                           out Int32 totalItems, ReportCaseSortOrder[] sortOrder)
        {
            return ReportHandler.GetReportCases(entity, sites, page, pageSize, out totalItems, sortOrder);
        }

        public virtual ReportCaseCollection GetReportCases(IReportableEntity entity, SiteCollection sites, NullValueAction siteNullAction,
                                                           Int32 page, Int32 pageSize, out Int32 totalItems, ReportCaseSortOrder[] sortOrder)
        {
            return ReportHandler.GetReportCases(entity, sites, siteNullAction, page, pageSize, out totalItems, sortOrder);
        }

        public virtual ReportCaseCollection GetReportCases(ReportCaseStatus status, IReportableEntity entity, SiteCollection sites,
                                                           Int32 page, Int32 pageSize, out Int32 totalItems, ReportCaseSortOrder[] sortOrder)
        {
            return ReportHandler.GetReportCases(status, entity, sites, page, pageSize, out totalItems, sortOrder);
        }

        public virtual ReportCaseCollection GetReportCases(ReportCaseStatus status, IReportableEntity entity, SiteCollection sites,
                                                           NullValueAction siteNullAction, Int32 page, Int32 pageSize, out Int32 totalItems,
                                                           ReportCaseSortOrder[] sortOrder)
        {
            return ReportHandler.GetReportCases(status, entity, sites, siteNullAction, page, pageSize, out totalItems, sortOrder);
        }

        public virtual ReportCaseCollection GetReportCases(ReportCaseStatus status, Type type, SiteCollection sites, Int32 page,
                                                           Int32 pageSize, out Int32 totalItems, ReportCaseSortOrder[] sortOrder)
        {
            return ReportHandler.GetReportCases(status, type, sites, page, pageSize, out totalItems, sortOrder);
        }

        public virtual ReportCaseCollection GetReportCases(ReportCaseStatus status, Type type, SiteCollection sites,
                                                           NullValueAction siteNullAction, Int32 page, Int32 pageSize, out Int32 totalItems,
                                                           ReportCaseSortOrder[] sortOrder)
        {
            return ReportHandler.GetReportCases(status, type, sites, siteNullAction, page, pageSize, out totalItems, sortOrder);
        }

        public virtual ReportCaseCollection GetReportCases(Type type, SiteCollection sites, Int32 page, Int32 pageSize, out Int32 totalItems,
                                                           ReportCaseSortOrder[] sortOrder)
        {
            return ReportHandler.GetReportCases(type, sites, page, pageSize, out totalItems, sortOrder);
        }

        public virtual ReportCaseCollection GetReportCases(Type type, SiteCollection sites, NullValueAction siteNullAction, Int32 page,
                                                           Int32 pageSize, out Int32 totalItems, ReportCaseSortOrder[] sortOrder)
        {
            return ReportHandler.GetReportCases(type, sites, siteNullAction, page, pageSize, out totalItems, sortOrder);
        }

        public virtual UserReportStatsCollection GetReportedAuthors(SiteCollection sites, NullValueAction siteNullAction, Int32 page,
                                                                    Int32 pageSize, out Int32 totalItems,
                                                                    UserReportStatsSortOrder[] sortOrder)
        {
            return ReportHandler.GetReportedAuthors(sites, siteNullAction, page, pageSize, out totalItems, sortOrder);
        }

        public virtual UserReportStatsCollection GetReportingUsers(SiteCollection sites, NullValueAction siteNullAction, Int32 page,
                                                                   Int32 pageSize, out Int32 totalItems,
                                                                   UserReportStatsSortOrder[] sortOrder)
        {
            return ReportHandler.GetReportingUsers(sites, siteNullAction, page, pageSize, out totalItems, sortOrder);
        }

        public virtual IUserReportStats GetUserReportStats(IUser user, UserReportStatsPerspective perspective, SiteCollection sites,
                                                           NullValueAction siteNullAction)
        {
            return ReportHandler.GetUserReportStats(user, perspective, sites, siteNullAction);
        }

        public virtual Boolean IsReported(IReportableEntity entity)
        {
            return ReportHandler.IsReported(entity);
        }

        #endregion

        public virtual void OnReportAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (ReportAdded != null)
                ReportAdded(sender, e);
        }

        public virtual void OnReportUpdated(String sender, EPiServerCommonEventArgs e)
        {
            if (ReportUpdated != null)
                ReportUpdated(sender, e);
        }

        public virtual void OnReportRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (ReportRemoved != null)
                ReportRemoved(sender, e);
        }

        public virtual void OnReportCaseUpdated(String sender, EPiServerCommonEventArgs e)
        {
            if (ReportCaseUpdated != null)
                ReportCaseUpdated(sender, e);
        }

        public virtual void OnReportCaseRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (ReportCaseRemoved != null)
                ReportCaseRemoved(sender, e);
        }
    }
}